package com.movie.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.movie.comm.utils.MD5;
import com.movie.comm.utils.PageData;
import com.movie.comm.utils.UuidUtil;
import com.movie.dao.UserDao;
import com.movie.db.util.DBHelper;
import com.movie.entity.User;

/**
 * 
 * @ClassName: MUserDaoImpl
 * @Description: Mysql类实现对User的增删改
 * @author 左岩
 * @date 2018年12月6日 下午3:41:14
 *
 */
public class MUserDaoImpl implements UserDao {

	
	@Override
	public List<User> getAllUser() {
		String sql = "select * from user";
		DBHelper dbHelper = new DBHelper();
		List<User> userList =null;
		try {
			userList = dbHelper.executeQuery(sql, null, User.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userList;
	}

	//根据用户Id删除用户
	@Override
	public boolean delUserById(PageData pageData) {
		String sql = "delete from user where userId = ?";
		ArrayList<Object> params = new ArrayList<>();
		if(null == pageData.getString("userId"))
			return false;
		params.add(pageData.getString("userId"));
		DBHelper dbHelper = new DBHelper();
		return dbHelper.excuteUpdate(sql, params);
	}

	//添加用户  没有进行非空校验
	@Override
	public boolean addUser(PageData pageData) {
		String sql = "insert into user values(?,?,?,?,?,?,?)";
		String userId = UuidUtil.get32UUID();
		String username = pageData.getString("username");
		String password = MD5.md5(pageData.getString("password"));
		Integer roleId = Integer.parseInt(pageData.getString("roleId"));
		Integer userCredit = Integer.parseInt(pageData.getString("userCredit"));
		Double userBalance = Double.parseDouble(pageData.getString("userBalance"));
		String userEmail = pageData.getString("userEmail");
		DBHelper dbHelper = new DBHelper();
		ArrayList<Object> params = new ArrayList<>();
		params.add(userId);
		params.add(username);
		params.add(password);
		params.add(roleId);
		params.add(userCredit);
		params.add(userBalance);
		params.add(userEmail);
		return dbHelper.excuteUpdate(sql, params);
	}
	
	

}
